package com.yaodian100.ec.serviceorder.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.ec.order.entity.InboundTrackingCase;
import com.yaodian100.ec.order.service.ServiceOrderService;

public class InboundTrackingCaseAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = -4029154178684376410L;
	private final Logger logger = LoggerFactory.getLogger(InboundTrackingCaseAction.class);

	private String orderIdParam;
	private String serviceIdParam;
	private String caseTypeParam;
	private String shipCompanyParam;
	private String shipNoParam;
	private Map<String, Object> trackCaseMap;
	private List<InboundTrackingCase> inboundTrackingCaseList;
	private InboundTrackingCase inboundTrackingCase;
	private String caseId;
	@Resource
	private ServiceOrderService serviceOrderService;

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
	}

	public String search() throws Exception {
		return "list";
	}

	public String list() throws Exception {
		trackCaseMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(orderIdParam)) {
			trackCaseMap.put("orderId", orderIdParam);
		}
		if (StringUtils.isNotBlank(serviceIdParam)) {
			trackCaseMap.put("serviceOrderId", serviceIdParam);
		}
		if (trackCaseMap.size() >= 1) {
			inboundTrackingCaseList = serviceOrderService.getInboundTrackingCase(trackCaseMap);
		}
		return "list";
	}

	public String add() throws Exception {
		return "add";
	}

	public String save() throws Exception {
		inboundTrackingCase = new InboundTrackingCase();
		inboundTrackingCase.setOrderId(orderIdParam);
		inboundTrackingCase.setServiceOrderId(serviceIdParam);
		inboundTrackingCase.setCaseType(Integer.parseInt(caseTypeParam));
		inboundTrackingCase.setShipCompany(shipCompanyParam);
		inboundTrackingCase.setShipNo(shipNoParam);
		trackCaseMap = new HashMap<String, Object>();
		trackCaseMap.put("orderId", orderIdParam);
		trackCaseMap.put("serviceOrderId", serviceIdParam);
		inboundTrackingCaseList = serviceOrderService.getInboundTrackingCase(trackCaseMap);
		if (inboundTrackingCaseList.size() == 0) {// 查無重覆，可新增。
			inboundTrackingCase = serviceOrderService.saveOrUpdateInboundTrackingCase(inboundTrackingCase);
			this.addActionError("储存成功");
		} else {
			this.addActionError("订单编号及服务单编号重覆");
		}
		return add();
	}

	public String view() throws Exception {
		inboundTrackingCase = new InboundTrackingCase();
		trackCaseMap = new HashMap<String, Object>();
		trackCaseMap.put("caseId", caseId);
		if (StringUtils.isBlank(caseId)) {
			addActionError("无此编号");
			return "view";
		}
		inboundTrackingCaseList = serviceOrderService.getInboundTrackingCase(trackCaseMap);

		if (null == inboundTrackingCaseList || inboundTrackingCaseList.size() == 0) {
			addActionError("无此编号");
			return "view";
		}

		if (inboundTrackingCaseList.size() > 0) {
			inboundTrackingCase = inboundTrackingCaseList.get(0);
		}
		return "view";
	}

	public String getOrderIdParam() {
		return orderIdParam;
	}

	public void setOrderIdParam(String orderIdParam) {
		this.orderIdParam = orderIdParam;
	}

	public String getServiceIdParam() {
		return serviceIdParam;
	}

	public void setServiceIdParam(String serviceIdParam) {
		this.serviceIdParam = serviceIdParam;
	}

	public String getCaseTypeParam() {
		return caseTypeParam;
	}

	public void setCaseTypeParam(String caseTypeParam) {
		this.caseTypeParam = caseTypeParam;
	}

	public String getShipCompanyParam() {
		return shipCompanyParam;
	}

	public void setShipCompanyParam(String shipCompanyParam) {
		this.shipCompanyParam = shipCompanyParam;
	}

	public String getShipNoParam() {
		return shipNoParam;
	}

	public void setShipNoParam(String shipNoParam) {
		this.shipNoParam = shipNoParam;
	}

	public ServiceOrderService getServiceOrderService() {
		return serviceOrderService;
	}

	public void setServiceOrderService(ServiceOrderService serviceOrderService) {
		this.serviceOrderService = serviceOrderService;
	}

	public List<InboundTrackingCase> getInboundTrackingCaseList() {
		return inboundTrackingCaseList;
	}

	public void setInboundTrackingCaseList(List<InboundTrackingCase> inboundTrackingCaseList) {
		this.inboundTrackingCaseList = inboundTrackingCaseList;
	}

	public Map<String, Object> getTrackCaseMap() {
		return trackCaseMap;
	}

	public void setTrackCaseMap(Map<String, Object> trackCaseMap) {
		this.trackCaseMap = trackCaseMap;
	}

	public InboundTrackingCase getInboundTrackingCase() {
		return inboundTrackingCase;
	}

	public void setInboundTrackingCase(InboundTrackingCase inboundTrackingCase) {
		this.inboundTrackingCase = inboundTrackingCase;
	}

	public String getCaseId() {
		return caseId;
	}

	public void setCaseId(String caseId) {
		this.caseId = caseId;
	}

}
